package org.jackysoft.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;

@Configuration
public class MongoConfiguration {

	private @Value("${mongo.dbname}") String  database;
	private @Value("${mongo.host}") String  host;
	private @Value("${mongo.port}") Integer  port=27017;
	private @Value("${mongo.user}") String  user;
	private @Value("${mongo.password}") String password;


	public String getDatabaseName() {
		return getDatabase();
		
		
	}

	
	public @Bean Mongo mongo() throws Exception {
		MongoOptions opts = new MongoOptions();
	    opts.autoConnectRetry = true;
	    opts.connectionsPerHost=50;
	    opts.threadsAllowedToBlockForConnectionMultiplier=10;
	    Mongo mon = new Mongo(getHost(),opts);
	    
		return mon;
	}
	
	public @Bean DB mongoDb() throws Exception {
	    Mongo mongo = mongo();
	    DB db = mongo.getDB(getDatabaseName());	   
	    return db;
	}
		
	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public Integer getPort() {
		return port;
	}

	public void setDatabase(String database) {
		this.database = database;
	}

	public String getDatabase() {
		return database;
	}

}
